import json

class BaiduProjectPipeline:
    def open_spider(self, spider):
        # 爬虫启动时打开文件
        self.file = open('baidu_data.json', 'w', encoding='utf-8')

    def close_spider(self, spider):
        # 爬虫结束时关闭文件
        self.file.close()

    def process_item(self, item, spider):
        # 将 item 转换为 JSON 并写入文件
        line = json.dumps(dict(item), ensure_ascii=False) + "\n"
        self.file.write(line)
        return item
